package health

import (
	"gitcode.com/G-YT/ANN/server/global"
	"gitcode.com/G-YT/ANN/server/model/common/request"
	"gitcode.com/G-YT/ANN/server/model/health"
	healthReq "gitcode.com/G-YT/ANN/server/model/health/request"
	"gorm.io/gorm"
)

type PhysiqueService struct {
}

// CreatePhysique 创建Physique记录
// Author [piexlmax](https://github.com/piexlmax)
func (physiqueService *PhysiqueService) CreatePhysique(physique *health.Physique) (err error) {
	err = global.GVA_DB.Create(physique).Error
	return err
}

// DeletePhysique 删除Physique记录
// Author [piexlmax](https://github.com/piexlmax)
func (physiqueService *PhysiqueService) DeletePhysique(physique health.Physique) (err error) {
	err = global.GVA_DB.Delete(&physique).Error
	return err
}

// DeletePhysiqueByIds 批量删除Physique记录
// Author [piexlmax](https://github.com/piexlmax)
func (physiqueService *PhysiqueService) DeletePhysiqueByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]health.Physique{}, "id in ?", ids.Ids).Error
	return err
}

// UpdatePhysique 更新Physique记录
// Author [piexlmax](https://github.com/piexlmax)
func (physiqueService *PhysiqueService) UpdatePhysique(physique health.Physique) (err error) {
	err = global.GVA_DB.Save(&physique).Error
	return err
}

// GetPhysique 根据id获取Physique记录
// Author [piexlmax](https://github.com/piexlmax)
func (physiqueService *PhysiqueService) GetPhysique(id uint) (physique health.Physique, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&physique).Error
	return
}

// GetPhysiqueId 根据体質名称获取体質ID
// Author [g-qs](https://github.com/g-qs)
func (physiqueService *PhysiqueService) GetPhysiqueId(name string) (physique health.Physique, err error) {
	err = global.GVA_DB.Where("tz_name LIKE ?", "%"+name+"%").First(&physique).Error
	return
}

// GetPhysiqueInfoList 分页获取Physique记录
// Author [piexlmax](https://github.com/piexlmax)
func (physiqueService *PhysiqueService) GetPhysiqueInfoList(info healthReq.PhysiqueSearch) (list []health.Physique, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&health.Physique{})
	var physiques []health.Physique
	// 如果有条件搜索 下方会自动创建搜索语句
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	if limit != 0 {
		db = db.Limit(limit).Offset(offset)
	}

	err = db.Find(&physiques).Error
	return physiques, total, err
}

func (physiqueService *PhysiqueService) SetPhysiqueGraphic(req healthReq.PhysiqueGraphicReq) (err error) {
	var physiqueArticle []healthReq.PhysiqueArticle
	return global.GVA_DB.Transaction(func(tx *gorm.DB) error {
		err = tx.Where("physique_id = ?", req.PhysiqueID).Delete(&physiqueArticle).Error
		if err != nil {
			return err
		}
		var physiqueArticle []healthReq.PhysiqueArticle
		for _, graphic := range req.Graphics {
			physiqueArticle = append(physiqueArticle, healthReq.PhysiqueArticle{
				PhysiqueID: req.PhysiqueID,
				ArticleID:  graphic,
				Type:       "graphic",
			})
		}
		for _, audio := range req.Audios {
			physiqueArticle = append(physiqueArticle, healthReq.PhysiqueArticle{
				PhysiqueID: req.PhysiqueID,
				ArticleID:  audio,
				Type:       "audio",
			})
		}
		for _, video := range req.Videos {
			physiqueArticle = append(physiqueArticle, healthReq.PhysiqueArticle{
				PhysiqueID: req.PhysiqueID,
				ArticleID:  video,
				Type:       "video",
			})
		}
		err = tx.Create(&physiqueArticle).Error
		return err
	})
}

func (physiqueService *PhysiqueService) GetPhysiqueGraphic(physique_id string) (physiqueGraphic []healthReq.PhysiqueArticle, err error) {
	err = global.GVA_DB.Where("physique_id = ?", physique_id).Find(&physiqueGraphic).Error
	return
}
